package org.ezplatform.office.rd.rcgl.web.rest;

import io.swagger.annotations.ApiOperation;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.ezplatform.component.iexp.export.util.ExportUtils;
import org.ezplatform.core.web.view.OperationResult;
import org.ezplatform.office.rd.rcgl.util.ConvertHtml2Excel;
import org.ezplatform.office.rd.sfdj.web.rest.SfdjController;
import org.ezplatform.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * @author gaoyw
 * @date 2024年10月30日 14:00
 * 日程管理
 */
@RestController
@RequestMapping(value = "/api/w/rd/rcgl")
public class RcglController {
    private static final Logger logger = LoggerFactory.getLogger(RcglController.class);

    @Resource
    private JdbcTemplate jdbcTemplate;

    @ApiOperation(value = "获取后台所有常委会领导下面的用户id", notes = "获取后台所有常委会领导下面的用户id")
    @RequestMapping(value = {"/getUserIdStr"}, method = {RequestMethod.POST})
    public OperationResult getUserIdStr() {
        String sql = "SELECT id FROM sys_user WHERE \"org_id\" ='b47ad977d1a44265a2d00458d76f8a9c' and \"is_delete\" ='0' ORDER BY \"sort_no\" asc ";
        List<Map<String, Object>> rlist = jdbcTemplate.queryForList(sql);
        String idStr="";
        if(rlist.size()>0){
            for(int a=0;a<rlist.size();a++) {
                Map amap = (Map) rlist.get(a);
                idStr += amap.get("id") + ",";
            }
        }
        if (idStr.endsWith(",")) {
            idStr = idStr.substring(0, idStr.length() - 1);
        }
        return OperationResult.buildSuccessResult(idStr);
    }

    @ApiOperation(value = "获取后台所有常委会领导下面的用户姓名", notes = "获取后台所有常委会领导下面的用户姓名")
    @RequestMapping(value = {"/getUserNameStr"}, method = {RequestMethod.POST})
    public OperationResult getUserNameStr() {
        String sql = "SELECT user_name FROM sys_user WHERE \"org_id\" ='b47ad977d1a44265a2d00458d76f8a9c' and \"is_delete\" ='0' ORDER BY \"sort_no\" asc ";
        List<Map<String, Object>> rlist = jdbcTemplate.queryForList(sql);
        return OperationResult.buildSuccessResult(rlist);
    }

    @ApiOperation(value = "获取所有领导的日程安排", notes = "获取所有领导的日程安排")
    @RequestMapping(value = {"/getLdCalendar"}, method = {RequestMethod.POST})
    public OperationResult getLdCalendar(String kssj,String jssj) {
        String sql = "SELECT user_name FROM sys_user WHERE \"org_id\" ='b47ad977d1a44265a2d00458d76f8a9c' and \"is_delete\" ='0' ORDER BY \"sort_no\" asc ";
        List<Map<String, Object>> rlist = jdbcTemplate.queryForList(sql);
        return OperationResult.buildSuccessResult(rlist);
    }


    @ApiOperation(value = "值班表导出", notes = "值班表导出")
    @RequestMapping(value = {"/exportTemplateData"}, method = {RequestMethod.POST})
    @ResponseBody
    public OperationResult exportTemplateData(HttpServletResponse response, HttpServletRequest request) {
        String content = StringUtils.null2String(request.getParameter("content"));
        String title = StringUtils.null2String(request.getParameter("title"));
        logger.error("contentcontentcontentcontent:"+content);
        logger.error("titletitletitletitletitle:"+title);
        content = content.replaceAll("%\">", "%\"></col>");//主要自动去掉了后缀
        String regex = "(?i)<td[^>]*>(?:\\s*?<.*?>)?([^<]*)</td>\\s*</tr>\\s*</table>";
        String replacedHtml = content.replaceAll(regex, "<td></td></tr></table>");
        logger.error("replacedHtml:" + replacedHtml);
        HSSFWorkbook workbook = ConvertHtml2Excel.table2Excel(replacedHtml);//表单内容转换为excel
        logger.error("workbook:" + workbook);
        OutputStream out = null;
        try {
            ExportUtils.responseContentDisposition(title + ".xls", request, response);
            out = response.getOutputStream();
            workbook.write(out);
            out.flush();
            out.close();
            //System.out.println("====================工信厅领导日程导出excel!" + title);
        } catch (IOException e) {

        }
        return null;
    }



}
